package org.pqsoft.fdemo.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.pqsoft.fdemo.service.FDemoService;

import com.mframework.annotation.aOperation;
import com.mframework.annotation.aResource;
import com.mframework.exception.mException;
import com.mframework.system.AbstractAction;
import com.mframework.system.PageTemplate;
import com.mframework.system.mSGA;

@aResource(name = "资源")
public class FDemoAction extends AbstractAction 
{
	//封装参数
	private Map<String, Object> getParameters()
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<?> name = request.getParameterNames();
		while (name.hasMoreElements()) {
			Object value = name.nextElement();
			String param = request.getParameter(value.toString()).trim();
			map.put(value.toString(), param);
		}
		return map;
	}
	
	FDemoService service = new FDemoService();

	//查看
	@aOperation(name ="select")
	public String select() throws mException 		
	{	
		Map<String,Object> maps = this.getParameters();
		
		PageTemplate pageTemplate = service.selectbp(maps);
		
		VelocityContext context = new VelocityContext(); 
		context.put("maps", maps);
		context.put("pageTemplate", pageTemplate);
		return mSGA.mergeVelocity("bp_select.vm",context );
	}
	
	//添加
	@aOperation(name ="insert")
	public  String doInsert() throws mException 
	{
		Map<String,Object> mapi = this.getParameters();	
		int i=service.insertbp(mapi);
	    return "添加成功"+i+"条数据";
	}

	public String insert() throws mException 
	{
		VelocityContext context = new VelocityContext();
		return mSGA.mergeVelocity("bp_insert.vm", context);
	}
	
	//更新
	@aOperation(name ="update")
	public String doUpdate() throws mException 
	{
		Map<String,Object> mapu = this.getParameters();	
		int i=service.updatebp(mapu);
		return "成功更新"+i+"条数据";
	}
	
	public String update() throws mException 
	{
		Map<String,Object> mapu = this.getParameters();
		VelocityContext context = new VelocityContext();
		Map mapup = service.selectbpup(mapu);
		context.put("mapup", mapup);
		return mSGA.mergeVelocity("bp_update.vm", context);
	}
	
	//删除
	@aOperation(name ="updelete")
	public String doUpDelete() throws mException 
	{
		Map<String,Object> mapud = this.getParameters();
		int i=service.updeletebp(mapud);
		return "成功删除"+i+"条数据";
	}
	
	//在数据库中删除
	@aOperation(name ="updelete")
	public String doDelete() throws mException 
	{
		Map<String,Object> mapud = this.getParameters();
		int i=service.deletebp(mapud);
		return "成功在数据库中删除"+i+"条数据";
	}
}
